package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumInfoDetailVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {


    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;





    //专辑详情
    @Override
    public AlbumInfoDetailVo getAlbumInfoDetail(Long albumId) {

        //判断该数据是否存在布隆过滤器中
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)) {
            log.info("该专辑信息不在布隆过滤器中");
            return null;
        }

        AlbumInfoDetailVo albumInfoDetailVo = new AlbumInfoDetailVo();

        //异步并排优化
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //通过专辑Id 获取专辑数据
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            AlbumInfo albumInfo = albumInfoResult.getData();
            albumInfoDetailVo.setAlbumInfo(albumInfo);
            return albumInfo;
        },threadPoolExecutor);

        CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
            //通过专辑Id 获取专辑统计信息
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.findAlbumStatByAlbumId(albumId);
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            albumInfoDetailVo.setAlbumStatVo(albumStatVo);
        },threadPoolExecutor);

        CompletableFuture<Void> completableFuture = albumInfoCompletableFuture.thenAcceptAsync((albumInfo) -> {
            //通过三级分类Id 获取到分类数据
            Long category3Id = albumInfo.getCategory3Id();
            Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.categoryfindAttribute(category3Id);
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            albumInfoDetailVo.setBaseCategoryView(baseCategoryView);
        },threadPoolExecutor);

        CompletableFuture<Void> completableFuture2 = albumInfoCompletableFuture.thenAcceptAsync((albumInfo) -> {
            // 4、根据专辑的用户的id查询专辑的作者数据
            Long userId = albumInfo.getUserId();
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.findUserInfoVoByUserId(userId);
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            albumInfoDetailVo.setAnnouncer(userInfoVo);
        },threadPoolExecutor);

        CompletableFuture.allOf(albumInfoCompletableFuture,completableFuture1,completableFuture,completableFuture2).join();

        return albumInfoDetailVo;
    }
}
